package boundary;

import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import org.jdesktop.swingx.JXTaskPaneContainer;
import org.jdesktop.swingx.JXTaskPane;
import org.jdesktop.swingx.action.AbstractActionExt;
import calculator.EsportaImporta;
import store.BaseStore;
import store.Cibo;
import store.Menu;
import store.Ricetta;
import store.TuttiCibi;
import dataview.Cibi;
import dataview.CriterioRicerca;
import dataview.Menus;
import dataview.Ricette;
import exceptions.DBException;
import executor.GestireCibi;
import executor.GestireImportExport;
import executor.GestireMenu;
import executor.GestireRicette;
import executor.Main;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
import javax.swing.BorderFactory;
import java.io.File;
import javax.swing.border.BevelBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;

public class Home extends BaseBoundary {
	
	private static final long serialVersionUID = 1L;
	public Main main;
	public GestireImportExport gie;  //  @jve:decl-index=0:
	public GestireCibi gc; // @jve:decl-index=0:
	public GestireRicette gr;
	public GestireMenu gm;
	public NuovoCibo nc;
	public ModificaInfoCibi mic;
	public ListaCibi lc;
	public ListaRicette lr;
	public ListaMenu lm;
	public NuovaRicetta nr;
	public ModificaInfoRicette mir;
	public ListaPassi lp;
	public RicercaCibi rc;
	public RicercaRicette rr;
	public RicercaMenu rm;
	public AttivareValutazioni av;
	public NuovoMenu nm;
	public ModificaInfoMenu mim;
	public ListaPortate listap;
	
	private String nCibo, nRicetta, nMenu; // @jve:decl-index=0:
	private Cibo cibo; // @jve:decl-index=0:
	private Ricetta ricetta; // @jve:decl-index=0:
	private Esporta esporta;
	
	private final static int WIDTH = 800, HEIGHT = 600;
	private JPanel jContentPane = null;
	private JXTaskPaneContainer JXTaskPaneContainer = null;
	private JXTaskPane JXTaskPaneCibi = null;
	private JXTaskPane JXTaskPaneRicette = null;
	private JXTaskPane JXTaskPaneMenu = null;
	private JLabel jLabel = null;
	private AbstractActionExt salvaAction;
	
	enum stati {
		DEFAULT, INSERIMENTO_NOME_CIBO, INSERIMENTO_INFOCIBI, ELIMINAZIONE_CIBO, MODIFICA_CIBO, INSERIMENTO_NOME_RICETTA, INSERIMENTO_INFORICETTA, INSERIMENTO_PROCEDIMENTO, INSERIMENTO_NOME_MENU, INSERIMENTO_INFOMENU, IMPORTAZIONE, MODIFICA_RICETTA, ELIMINAZIONE_RICETTA, ELIMINAZIONE_MENU, MODIFICA_MENU, INSERIMENTO_PORTATE, 
		ESPORTAZIONE
	};
	
	private stati stato; // @jve:decl-index=0:
	private JXTaskPane JXTaskPaneExpImp = null;
	private Menu menu; // @jve:decl-index=0:
	private JXTaskPane JXTaskPaneFile = null;
	private JScrollPane jScrollPane = null; //  @jve:decl-index=0:visual-constraint="668,205"
	
	void setStato(stati s) {
		String title = "EasyCooking";
		if (BaseStore.isModificato()) {
			salvaAction.setEnabled(true);
			title = "* " + title;
		} else salvaAction.setEnabled(false);
		stato = s;
		switch (s) {
			case INSERIMENTO_NOME_CIBO:
				title += " - Inserimento nuovo cibo";
				break;
			case INSERIMENTO_NOME_RICETTA:
				title += " - Inserimento nuova ricetta";
				break;
			case INSERIMENTO_NOME_MENU:
				title += " - Inserimento nuovo menu";
				break;
			case INSERIMENTO_INFOCIBI:
				title += " - Inserimento dati cibo";
				break;
			case INSERIMENTO_INFORICETTA:
				title += " - Inserimento dati ricetta";
				break;
			case INSERIMENTO_INFOMENU:
				title += " - Inserimento dati menu";
				break;
			case MODIFICA_CIBO:
				title += " - Modifica dati cibo";
				break;
			case MODIFICA_RICETTA:
				title += " - Modifica dati ricetta";
				break;
			case MODIFICA_MENU:
				title += " - Modifica dati menu";
				break;
			case INSERIMENTO_PROCEDIMENTO:
				title += " - Inserimento procedimento";
				break;
			case INSERIMENTO_PORTATE:
				title += " - Inserimento portate";
				break;
			case ESPORTAZIONE:
				title += " - Esportazione dati";
				break;
			default:
				break;
		}
		setTitle(title);
	}
	
	/**
	 * This is the default constructor
	 */
	public Home(Main main) {
		super();
		this.main = main;
		initialize();
		setStato(stati.DEFAULT);
	}
	
	/**
	 * This method returns the reference to the Home object
	 * 
	 * @return Home
	 */
	private Home getHome() {
		return this;
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (screen.width - WIDTH) / 2;
		int y = (screen.height - HEIGHT) / 2;
		this.setSize(WIDTH, HEIGHT);
		this.setLocation(x, y);
		this.setResizable(true);
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icone/food-icon.png")));
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setContentPane(getJContentPane());
		this.setTitle("EasyCooking");
		this.setVisible(true);
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				esci();
			}
		});
	}
	
	private void esci() {
		if (BaseStore.isModificato()) {
			switch (JOptionPane.showConfirmDialog(getHome(), "Si vogliono salvare tutte le modifiche apportate nella sessione corrente?", "Chiusura Applicazione", JOptionPane.INFORMATION_MESSAGE)) {
				case JOptionPane.YES_OPTION:
					try {
						BaseStore.save(true);
					} catch (DBException e1) {
						e1.printStackTrace();
					}
				case JOptionPane.NO_OPTION:
					new File(BaseStore.DB_TMP).delete();
					System.exit(0);
			}
		} else System.exit(0);
	}
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel = new JLabel();
			jLabel.setText("");
			jLabel.setPreferredSize(new Dimension(590, 200));
			jLabel.setOpaque(false);
			jLabel.setIcon(new ImageIcon(getClass().getResource("/icone/home2.png")));
			jLabel.setVisible(true);
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
			jContentPane.add(getJScrollPane(), BorderLayout.WEST);
			jContentPane.add(jLabel, BorderLayout.EAST);
		}
		return jContentPane;
	}
	
	/**
	 * This method initializes JXTaskPaneContainer
	 * 
	 * @return org.jdesktop.swingx.JXTaskPaneContainer
	 */
	private JXTaskPaneContainer getJXTaskPaneContainer() {
		if (JXTaskPaneContainer == null) {
			JXTaskPaneContainer = new JXTaskPaneContainer();
			JXTaskPaneContainer.setPreferredSize(new Dimension(200, 190));
			JXTaskPaneContainer.add(getJXTaskPaneFile(), null);
			JXTaskPaneContainer.add(getJXTaskPaneCibi(), null);
			JXTaskPaneContainer.add(getJXTaskPaneRicette(), null);
			JXTaskPaneContainer.add(getJXTaskPaneMenu(), null);
			JXTaskPaneContainer.add(getJXTaskPaneExpImp(), null);
		}
		return JXTaskPaneContainer;
	}
	
	/**
	 * This method initializes JXTaskPaneCibi
	 * 
	 * @return org.jdesktop.swingx.JXTaskPane
	 */
	private JXTaskPane getJXTaskPaneCibi() {
		if (JXTaskPaneCibi == null) {
			JXTaskPaneCibi = new JXTaskPane();
			JXTaskPaneCibi.setTitle("Cibi");
			JXTaskPaneCibi.setCollapsed(true);
			JXTaskPaneCibi.setIcon(new ImageIcon(getClass().getResource("/icone/application-edit-icon.png")));
			JXTaskPaneCibi.add(new AbstractActionExt("Aggiungi un cibo", new ImageIcon(getClass().getResource("/icone/icon-add.png"))) {
				private static final long serialVersionUID = -1960928737901078767L;
				
				@Override
				public void actionPerformed(ActionEvent e) {
					jLabel.setVisible(false);
					nc = new NuovoCibo(getHome());
					nc.setAlignmentY(TOP_ALIGNMENT);
					nc.setAlignmentX(LEFT_ALIGNMENT);
					nascondiCentro();
					getJContentPane().add(nc, BorderLayout.CENTER);
					setStato(stati.INSERIMENTO_NOME_CIBO);
					nc.setVisible(true);
					nc.focus();
				}
			});
			JXTaskPaneCibi.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
				public void propertyChange(java.beans.PropertyChangeEvent e) {
					if ((e.getPropertyName().equals("collapsed"))) {
						if ((Boolean)e.getNewValue() == false) {
							jLabel.setVisible(false);
							collassaAltri(e.getSource());
							main.passaACibi();
							lc.setTitle("Risultati Ultima Ricerca");
							setStato(stati.DEFAULT);
						}
					}
				}
			});
			JXTaskPaneCibi.add(new AbstractActionExt("Lista completa", new ImageIcon(getClass().getResource("/icone/calendar-list.png"))) {
				private static final long serialVersionUID = 1L;
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					jLabel.setVisible(false);
					mostraListaCibi(null);
				}
				
			});
			JXTaskPaneCibi.add(new AbstractActionExt("Ricerca cibi", new ImageIcon(getClass().getResource("/icone/icon_search-1.png"))) {
				private static final long serialVersionUID = 1L;
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					jLabel.setVisible(false);
					rc = new RicercaCibi(getHome());
					rc.setAlignmentY(TOP_ALIGNMENT);
					rc.setAlignmentX(LEFT_ALIGNMENT);
					nascondiCentro();
					getJContentPane().add(rc, BorderLayout.CENTER);
					rc.setVisible(true);
					setStato(stati.DEFAULT);
				}
				
			});
		}
		return JXTaskPaneCibi;
	}
	
	/**
	 * This method initializes JXTaskPaneRicette
	 * 
	 * @return org.jdesktop.swingx.JXTaskPane
	 */
	private JXTaskPane getJXTaskPaneRicette() {
		if (JXTaskPaneRicette == null) {
			JXTaskPaneRicette = new JXTaskPane();
			JXTaskPaneRicette.setTitle("Ricette");
			JXTaskPaneRicette.setCollapsed(true);
			JXTaskPaneRicette.setIcon(new ImageIcon(getClass().getResource("/icone/notes-reedit-icon.png")));
			JXTaskPaneRicette.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
				public void propertyChange(java.beans.PropertyChangeEvent e) {
					if ((e.getPropertyName().equals("collapsed"))) {
						if ((Boolean)e.getNewValue() == false) {
							jLabel.setVisible(false);
							collassaAltri(e.getSource());
							main.passaARicette();
							lr.setTitle("Risultati Ultima Ricerca");
							setStato(stati.DEFAULT);
						}
					}
				}
			});
			JXTaskPaneRicette.add(new AbstractActionExt("Aggiungi una ricetta", new ImageIcon(getClass().getResource("/icone/icon-add.png"))) {
				private static final long serialVersionUID = 1L;
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					try {
						if (TuttiCibi.getTuttiCibi().tuttiCibi().size() == 0) {
							JOptionPane.showMessageDialog(getHome(), "Attenzione, non puoi inserire una ricetta se nel database non ci sono cibi", "Attenzione", JOptionPane.WARNING_MESSAGE);
							return;
						}
					} catch (DBException e) {
						e.printStackTrace();
						System.exit(111);
					}
					jLabel.setVisible(false);
					nr = new NuovaRicetta(getHome());
					nr.setAlignmentY(TOP_ALIGNMENT);
					nr.setAlignmentX(LEFT_ALIGNMENT);
					nascondiCentro();
					getJContentPane().add(nr, BorderLayout.CENTER);
					setStato(stati.INSERIMENTO_NOME_RICETTA);
					nr.setVisible(true);
					nr.focus();
				}
				
			});
			JXTaskPaneRicette.add(new AbstractActionExt("Lista completa", new ImageIcon(getClass().getResource("/icone/calendar-list.png"))) {
				private static final long serialVersionUID = 1L;
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					jLabel.setVisible(false);
					mostraListaRicette(null);
				}
				
			});
			JXTaskPaneRicette.add(new AbstractActionExt("Ricerca ricette", new ImageIcon(getClass().getResource("/icone/icon_search-1.png"))) {
				private static final long serialVersionUID = 1L;
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					jLabel.setVisible(false);
					rr = new RicercaRicette(getHome());
					rr.setAlignmentY(TOP_ALIGNMENT);
					rr.setAlignmentX(LEFT_ALIGNMENT);
					nascondiCentro();
					getJContentPane().add(rr, BorderLayout.CENTER);
					rr.setVisible(true);
					setStato(stati.DEFAULT);
				}
			});
			JXTaskPaneRicette.add(new AbstractActionExt("Attivare valutazioni ricette", new ImageIcon(getClass().getResource("/icone/settings.png"))) {
				private static final long serialVersionUID = 8373057127595236271L;
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					jLabel.setVisible(false);
					av = new AttivareValutazioni(getHome(), true);
					av.setAlignmentY(TOP_ALIGNMENT);
					av.setAlignmentX(LEFT_ALIGNMENT);
					nascondiCentro();
					getJContentPane().add(av, BorderLayout.CENTER);
					av.setVisible(true);
					setStato(stati.DEFAULT);
				}
			});
		}
		return JXTaskPaneRicette;
	}
	
	/**
	 * This method initializes JXTaskPaneMenu
	 * 
	 * @return org.jdesktop.swingx.JXTaskPane
	 */
	private JXTaskPane getJXTaskPaneMenu() {
		if (JXTaskPaneMenu == null) {
			JXTaskPaneMenu = new JXTaskPane();
			JXTaskPaneMenu.setTitle("Menu");
			JXTaskPaneMenu.setCollapsed(true);
			JXTaskPaneMenu.setIcon(new ImageIcon(getClass().getResource("/icone/blog-post-edit-icon.png")));
			JXTaskPaneMenu.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
				public void propertyChange(java.beans.PropertyChangeEvent e) {
					if ((e.getPropertyName().equals("collapsed"))) {
						if ((Boolean)e.getNewValue() == false) {
							jLabel.setVisible(false);
							collassaAltri(e.getSource());
							main.passaAMenu();
							lm.setTitle("Risultati Ultima Ricerca");
							setStato(stati.DEFAULT);
						}
					}
				}
			});
			JXTaskPaneMenu.add(new AbstractActionExt("Aggiungi un menu", new ImageIcon(getClass().getResource("/icone/icon-add.png"))) {
				private static final long serialVersionUID = 1L;
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					try {
						if (TuttiCibi.getTuttiCibi().tuttiCibi().size() == 0) {
							JOptionPane.showMessageDialog(getHome(), "Attenzione, non puoi inserire un menu se nel database non ci sono ne` cibi ne` ricette", "Attenzione", JOptionPane.WARNING_MESSAGE);
							return;
						}
					} catch (DBException e) {
						e.printStackTrace();
						System.exit(111);
					}
					jLabel.setVisible(false);
					nm = new NuovoMenu(getHome());
					nm.setAlignmentY(TOP_ALIGNMENT);
					nm.setAlignmentX(LEFT_ALIGNMENT);
					nascondiCentro();
					getJContentPane().add(nm, BorderLayout.CENTER);
					setStato(stati.INSERIMENTO_NOME_MENU);
					nm.setVisible(true);
					nm.focus();
				}
				
			});
			JXTaskPaneMenu.add(new AbstractActionExt("Lista completa", new ImageIcon(getClass().getResource("/icone/calendar-list.png"))) {
				private static final long serialVersionUID = 1L;
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					jLabel.setVisible(false);
					mostraListaMenu(null);
				}
				
			});
			JXTaskPaneMenu.add(new AbstractActionExt("Ricerca menu", new ImageIcon(getClass().getResource("/icone/icon_search-1.png"))) {
				private static final long serialVersionUID = 1L;
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					jLabel.setVisible(false);
					rm = new RicercaMenu(getHome());
					rm.setAlignmentY(TOP_ALIGNMENT);
					rm.setAlignmentX(LEFT_ALIGNMENT);
					nascondiCentro();
					getJContentPane().add(rm, BorderLayout.CENTER);
					rm.setVisible(true);
					setStato(stati.DEFAULT);
				}
				
			});
			JXTaskPaneMenu.add(new AbstractActionExt("Attivare valutazioni menu", new ImageIcon(getClass().getResource("/icone/settings.png"))) {
				private static final long serialVersionUID = 7198082271023535604L;
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					jLabel.setVisible(false);
					av = new AttivareValutazioni(getHome(), false);
					av.setAlignmentY(TOP_ALIGNMENT);
					av.setAlignmentX(LEFT_ALIGNMENT);
					nascondiCentro();
					getJContentPane().add(av, BorderLayout.CENTER);
					av.setVisible(true);
					setStato(stati.DEFAULT);
				}
			});
		}
		return JXTaskPaneMenu;
	}
	
	/**
	 * This method initializes JXTaskPaneExpImp
	 * 
	 * @return org.jdesktop.swingx.JXTaskPane
	 */
	private JXTaskPane getJXTaskPaneExpImp() {
		if (JXTaskPaneExpImp == null) {
			JXTaskPaneExpImp = new JXTaskPane();
			JXTaskPaneExpImp.setTitle("Importa / Esporta");
			JXTaskPaneExpImp.setCollapsed(true);
			JXTaskPaneExpImp.setIcon(new ImageIcon(getClass().getResource("/icone/page-up-down-icon.png")));
			JXTaskPaneExpImp.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
				public void propertyChange(java.beans.PropertyChangeEvent e) {
					if ((e.getPropertyName().equals("collapsed"))) {
						if ((Boolean)e.getNewValue() == false) {
							collassaAltri(e.getSource());
							main.passaAImpExp();
							setStato(stati.DEFAULT);
						}
					}
				}
			});
			JXTaskPaneExpImp.add(new AbstractActionExt("Importa dati", new ImageIcon(getClass().getResource("/icone/import1.gif"))) {
				private static final long serialVersionUID = 7198082271023535604L;
				
				@Override
				public void actionPerformed(ActionEvent e) {
					getHome().setWaiting(true);
					setStato(stati.IMPORTAZIONE);
					JFileChooser importa = new JFileChooser();
					importa.setApproveButtonText("Importa");
					importa.setControlButtonsAreShown(true);
					importa.setCurrentDirectory(new File(EsportaImporta.EXPORT_DIR));
					importa.addChoosableFileFilter(new FileFilter() {
						@Override
						public String getDescription() {
							return "EasyCooking File (*.ecd)";
						}
						
						@Override
						public boolean accept(File f) {
							return (f.isDirectory() || f.getAbsolutePath().endsWith(".ecd"));
						}
					});
					int rix = importa.showOpenDialog(getJContentPane());
					getHome().setWaiting(false);
					if (rix == JFileChooser.APPROVE_OPTION) {
						getHome().setWaiting(true);
						gie.importa(importa.getSelectedFile());
						mostraListaCibi(null);
					}
					setStato(stati.DEFAULT);
				}
			});
			JXTaskPaneExpImp.add(new AbstractActionExt("Esporta dati", new ImageIcon(getClass().getResource("/icone/export.png"))) {
				private static final long serialVersionUID = 6209669086922839828L;
				
				@Override
				public void actionPerformed(ActionEvent e) {
					esporta = new Esporta(getHome());
					esporta.setAlignmentY(TOP_ALIGNMENT);
					esporta.setAlignmentX(LEFT_ALIGNMENT);
					jLabel.setVisible(false);
					nascondiCentro();
					getJContentPane().add(esporta, BorderLayout.CENTER);
					setStato(stati.ESPORTAZIONE);
					esporta.setVisible(true);
					getHome().setVisible(true);
					esporta.requestFocus();
				}
			});
		}
		return JXTaskPaneExpImp;
	}
	
	public void richiediInfoCibo(String nCibo) {
		nc.setVisible(false);
		mic = new ModificaInfoCibi(this, nCibo);
		mic.setAlignmentY(TOP_ALIGNMENT);
		mic.setAlignmentX(LEFT_ALIGNMENT);
		getJContentPane().add(mic, BorderLayout.CENTER);
	}
	
	public void richiediInfoRicetta(Ricetta ricetta) {
		nascondiCentro();
		setStato(stati.INSERIMENTO_INFORICETTA);
		mir = new ModificaInfoRicette(this, ricetta);
		mir.setAlignmentY(TOP_ALIGNMENT);
		mir.setAlignmentX(LEFT_ALIGNMENT);
		getJContentPane().add(mir, BorderLayout.CENTER);
	}
	
	public void richiediInfoMenu(Menu m) {
		nascondiCentro();
		setStato(stati.INSERIMENTO_INFOMENU);
		mim = new ModificaInfoMenu(this, m);
		mim.setAlignmentY(TOP_ALIGNMENT);
		mim.setAlignmentX(LEFT_ALIGNMENT);
		getJContentPane().add(mim, BorderLayout.CENTER);
	}
	
	private void collassaAltri(Object eccetto) {
		if (!getJXTaskPaneCibi().equals(eccetto) && !getJXTaskPaneCibi().isCollapsed()) getJXTaskPaneCibi().setCollapsed(true);
		if (!getJXTaskPaneRicette().equals(eccetto) && !getJXTaskPaneRicette().isCollapsed()) getJXTaskPaneRicette().setCollapsed(true);
		if (!getJXTaskPaneMenu().equals(eccetto) && !getJXTaskPaneMenu().isCollapsed()) getJXTaskPaneMenu().setCollapsed(true);
		if (!getJXTaskPaneExpImp().equals(eccetto) && !getJXTaskPaneExpImp().isCollapsed()) getJXTaskPaneExpImp().setCollapsed(true);
	}
	
	private void nascondiCentro() {
		Component c = ((BorderLayout)getJContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER);
		if (c != null) c.setVisible(false);
	}
	
	public void inserireCibo(String nCibo) {
		try {
			this.nCibo = nCibo;
			gc.inserireCibo(nCibo);
		} catch (DBException e) {
			e.printStackTrace();
		}
	}
	
	public void eliminareCibo(Cibo cibo) {
		setStato(stati.ELIMINAZIONE_CIBO);
		this.cibo = cibo;
		gc.eliminareCibo(cibo);
	}
	
	public void eliminareRicetta(Ricetta ricetta) {
		setStato(stati.ELIMINAZIONE_RICETTA);
		this.ricetta = ricetta;
		gr.eliminareRicetta(ricetta);
	}
	
	public void eliminareMenu(Menu m) {
		setStato(stati.ELIMINAZIONE_MENU);
		this.menu = m;
		gm.eliminareMenu(m);
	}
	
	public void modificareCibo(Cibo cibo) {
		setStato(stati.MODIFICA_CIBO);
		nascondiCentro();
		mic = new ModificaInfoCibi(this, cibo);
		mic.setAlignmentY(TOP_ALIGNMENT);
		mic.setAlignmentX(LEFT_ALIGNMENT);
		getJContentPane().add(mic, BorderLayout.CENTER);
	}
	
	public void modificareRicetta(Ricetta ricetta) {
		setStato(stati.MODIFICA_RICETTA);
		nascondiCentro();
		mir = new ModificaInfoRicette(this, ricetta);
		mir.setAlignmentY(TOP_ALIGNMENT);
		mir.setAlignmentX(LEFT_ALIGNMENT);
		getJContentPane().add(mir, BorderLayout.CENTER);
	}
	
	public void modificareMenu(Menu menu) {
		setStato(stati.MODIFICA_MENU);
		nascondiCentro();
		mim = new ModificaInfoMenu(this, menu);
		mim.setAlignmentY(TOP_ALIGNMENT);
		mim.setAlignmentX(LEFT_ALIGNMENT);
		getJContentPane().add(mim, BorderLayout.CENTER);
	}
	
	public void ricercareCibo(CriterioRicerca criterio) {
		try {
			gc.ricercare(criterio);
		} catch (DBException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, "Impossibile cercare, database non funzionante", "Errore", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public void ricercareRicetta(CriterioRicerca criterio) {
		try {
			gr.ricercare(criterio);
		} catch (DBException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, "Impossibile cercare, database non funzionante", "Errore", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public void ricercareMenu(CriterioRicerca criterio) {
		try {
			gm.ricercare(criterio);
		} catch (DBException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, "Impossibile cercare, database non funzionante", "Errore", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public void passaCibi(Cibi cibiRisultato) {
		mostraListaCibi(cibiRisultato);
	}
	
	public void passaRicette(Ricette ricetteRisultato) {
		mostraListaRicette(ricetteRisultato);
	}
	
	public void passaMenu(Menus menuRisultato) {
		mostraListaMenu(menuRisultato);
	}
	
	public void creaAccedereCibi(GestireCibi gc, Cibi ultimiCercati) {
		this.gc = gc;
		nascondiCentro();
		mostraListaCibi(ultimiCercati);
	}
	
	public void creaAccedereRicette(GestireRicette gr, Ricette ultimeCercate) {
		this.gr = gr;
		nascondiCentro();
		mostraListaRicette(ultimeCercate);
	}
	
	public void creaAccedereMenu(GestireMenu gm, Menus ultimiCercati) {
		this.gm = gm;
		nascondiCentro();
		mostraListaMenu(ultimiCercati);
	}
	
	public void mostraListaCibi(Cibi cibi) {
		nascondiCentro();
		if (lc == null)
			lc = new ListaCibi(getHome(), cibi);
		else lc.setCibi(cibi);
		getJContentPane().add(lc, BorderLayout.CENTER);
		lc.aggiornaVista();
		lc.setVisible(true);
		setStato(stati.DEFAULT);
	}
	
	public void mostraListaRicette(Ricette ricette) {
		nascondiCentro();
		if (lr == null) //
			lr = new ListaRicette(this, ricette);
		else
		//
		lr.setRicette(ricette); //
		getJContentPane().add(lr, BorderLayout.CENTER);
		lr.setVisible(true);
		setStato(stati.DEFAULT);
	}
	
	public void mostraListaMenu(Menus menu) {
		nascondiCentro();
		if (lm == null) //
			lm = new ListaMenu(this, menu);
		else
		//
		lm.setMenu(menu); //
		getJContentPane().add(lm, BorderLayout.CENTER);
		lm.setVisible(true);
		setStato(stati.DEFAULT);
	}
	
	public void inserireRicetta(String nomeR) {
		nRicetta = nomeR;
		gr.inserireRicetta(nRicetta);
	}
	
	public void visualizzaProcedimento(Ricetta r) {
		nascondiCentro();
		setStato(stati.INSERIMENTO_PROCEDIMENTO);
		lp = new ListaPassi(this, r);
		getJContentPane().add(lp, BorderLayout.CENTER);
		lp.setVisible(true);
	}
	
	public void visualizzaPortate(Menu m) {
		nascondiCentro();
		setStato(stati.INSERIMENTO_PORTATE);
		listap = new ListaPortate(this, m);
		getJContentPane().add(listap, BorderLayout.CENTER);
		listap.setVisible(true);
	}
	
	public void inserireMenu(String nomeM) {
		nMenu = nomeM;
		gm.inserireMenu(nMenu);
	}
	
	public void ko() {
		switch (stato) {
			case ELIMINAZIONE_CIBO:
				JOptionPane.showMessageDialog(this, "Impossibile eliminare il cibo. Il cibo selezionato e' in uso da una ricetta e/o da un menu", "Eliminazione Cibo Fallita", JOptionPane.ERROR_MESSAGE);
				setStato(stati.DEFAULT);
				break;
			case ELIMINAZIONE_RICETTA:
				JOptionPane.showMessageDialog(this, "Impossibile eliminare la ricetta. La ricetta selezionata e' in uso da un'altra ricetta e/o da un menu", "Eliminazione Ricetta Fallita", JOptionPane.ERROR_MESSAGE);
				setStato(stati.DEFAULT);
				break;
		}
	}
	
	public void ok() {
		switch (stato) {
			case ELIMINAZIONE_CIBO:
				int res = JOptionPane.showConfirmDialog(this, "Sei sicuro di voler eliminare " + cibo.getNome() + "?", "Conferma", JOptionPane.YES_NO_OPTION);
				if (res == JOptionPane.YES_OPTION) {
					try {
						gc.conferma();
						JOptionPane.showMessageDialog(this, "Cibo eliminato correttamente", "Eliminazione cibo", JOptionPane.INFORMATION_MESSAGE);
					} catch (Exception e) {
						JOptionPane.showMessageDialog(this, "Impossibile eliminare il cibo", "Eliminazione cibo", JOptionPane.ERROR_MESSAGE);
					}
				} else {
					gc.annulla();
				}
				setStato(stati.DEFAULT);
				break;
			
			case ELIMINAZIONE_RICETTA:
				int res2 = JOptionPane.showConfirmDialog(this, "Sei sicuro di voler eliminare " + ricetta.getNome() + "?", "Conferma", JOptionPane.YES_NO_OPTION);
				if (res2 == JOptionPane.YES_OPTION) {
					try {
						gr.conferma();
						JOptionPane.showMessageDialog(this, "Ricetta eliminato correttamente", "Eliminazione ricetta", JOptionPane.INFORMATION_MESSAGE);
					} catch (Exception e) {
						JOptionPane.showMessageDialog(this, "Impossibile eliminare la ricetta", "Eliminazione ricetta", JOptionPane.ERROR_MESSAGE);
					}
				} else {
					gr.annulla();
				}
				setStato(stati.DEFAULT);
				break;
			
			case ELIMINAZIONE_MENU:
				int res3 = JOptionPane.showConfirmDialog(this, "Sei sicuro di voler eliminare " + menu.getNome() + "?", "Conferma", JOptionPane.YES_NO_OPTION);
				if (res3 == JOptionPane.YES_OPTION) {
					try {
						gm.conferma();
						JOptionPane.showMessageDialog(this, "Menu eliminato correttamente", "Eliminazione menu", JOptionPane.INFORMATION_MESSAGE);
					} catch (Exception e) {
						JOptionPane.showMessageDialog(this, "Impossibile eliminare il menu", "Eliminazione menu", JOptionPane.ERROR_MESSAGE);
					}
				}
				setStato(stati.DEFAULT);
				break;
		}
	}
	
	public void fatto() {
		switch (stato) {
			case INSERIMENTO_NOME_CIBO:
				setStato(stati.INSERIMENTO_INFOCIBI);
				richiediInfoCibo(nCibo);
				break;
			case INSERIMENTO_INFOCIBI:
			case MODIFICA_CIBO:
				setStato(stati.DEFAULT);
				mostraListaCibi(null);
				break;
			case IMPORTAZIONE:
				JOptionPane.showMessageDialog(this, "Importazione riuscita", "Operazione Terminata", JOptionPane.INFORMATION_MESSAGE);
				setStato(stati.DEFAULT);
				break;
		}
	}
	
	public void fallito() {
		switch (stato) {
			case INSERIMENTO_NOME_CIBO:
				JOptionPane.showMessageDialog(this, "Spiacente, cibo gia` presente", "Errore", JOptionPane.ERROR_MESSAGE);
				break;
			case INSERIMENTO_NOME_RICETTA:
				JOptionPane.showMessageDialog(this, "Spiacente, ricetta gia` presente", "Errore", JOptionPane.ERROR_MESSAGE);
				break;
			case INSERIMENTO_NOME_MENU:
				JOptionPane.showMessageDialog(this, "Spiacente, menu  gia` presente", "Errore", JOptionPane.ERROR_MESSAGE);
				break;
			case IMPORTAZIONE:
				JOptionPane.showMessageDialog(this, "Errore nell'importazione del file, formato del file non valido", "Errore importazione", JOptionPane.ERROR_MESSAGE);
				setStato(stati.DEFAULT);
				break;
		}
	}
	
	public void creaAccedereImportExport(GestireImportExport gestireImportExport) {
		this.gie = gestireImportExport;
	}
	
	/**
	 * This method initializes JXTaskPaneFile
	 * 
	 * @return org.jdesktop.swingx.JXTaskPane
	 */
	private JXTaskPane getJXTaskPaneFile() {
		if (JXTaskPaneFile == null) {
			JXTaskPaneFile = new JXTaskPane();
			JXTaskPaneFile.setSpecial(true);
			JXTaskPaneFile.setTitle("File");
			salvaAction = new AbstractActionExt("Salva", new ImageIcon(getClass().getResource("/icone/save_16.gif"))) {
				private static final long serialVersionUID = -131578062831623222L;
				
				@Override
				public void actionPerformed(ActionEvent e) {
					if (JOptionPane.showConfirmDialog(getHome(), "Salvare le modifiche?", "Salva", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) try {
						BaseStore.save(true);
						if (getTitle().startsWith("* ")) setTitle(getTitle().substring(2));
						new File(BaseStore.DB_TMP).delete();
						salvaAction.setEnabled(false);
					} catch (DBException e1) {
						e1.printStackTrace();
					}
				}
			};
			JXTaskPaneFile.add(salvaAction);
			JXTaskPaneFile.add(new AbstractActionExt("Opzioni", new ImageIcon(getClass().getResource("/icone/preferences16_h.gif"))) {
				private static final long serialVersionUID = -2578257343141829699L;
				
				@Override
				public void actionPerformed(ActionEvent e) {
					Opzioni o = new Opzioni(getHome());
					o.setVisible(true);
				}
			});
			JXTaskPaneFile.add(new AbstractActionExt("Esci da EasyCooking", new ImageIcon(getClass().getResource("/icone/exit2.png"))) {
				private static final long serialVersionUID = -1736331832549235500L;
				
				@Override
				public void actionPerformed(ActionEvent e) {
					esci();
				}
			});
		}
		return JXTaskPaneFile;
	}
	
	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJXTaskPaneContainer());
		}
		return jScrollPane;
	}
} // @jve:decl-index=0:visual-constraint="0,0"
